package com.briup.springboot_mybatisplus.config;

import com.baomidou.mybatisplus.generator.FastAutoGenerator;
import com.baomidou.mybatisplus.generator.config.OutputFile;
import com.baomidou.mybatisplus.generator.config.po.LikeTable;
import com.baomidou.mybatisplus.generator.engine.FreemarkerTemplateEngine;

import java.util.Arrays;
import java.util.Collections;

public class MybatisPlusGeneratorConfig {
    /*数据库连接信息*/
    private static String url = "jdbc:mysql://127.0.0.1:3306/testdb";
    private static String username = "root";
    private static String password = "root";
    //项目所在磁盘目录
    private static String basePath = "D:\\IdeaProjects\\springboot_mybatisplus\\";
    //设置包和代码指定输出目录
    private static String filePath = basePath+"src\\main\\java";
    //xml映射文件输出目录
    private static String mapperXmlPath =
            basePath+"src\\main\\resources\\mapper";
    //指定哪些前缀的表生成代码,支持匹配多个前缀
    private static String tablePrefix = "sys";
    //文注注释显示作者信息
    private static String author = "briup";
    //项目父包名
    private static String parentPackage = "com.briup.springboot_mybatisplus";
    private static String controllerPackage = "web.controller";
    private static String entityPackage = "bean";
    private static String mapperPackage = "dao";
    public static void main(String[] args) {
        FastAutoGenerator.create(url,username,password)
                .globalConfig(builder -> {
                    builder.author(author)
                            .disableOpenDir() //生成文件后不自动打开所在目录
                            .outputDir(filePath);
                }).packageConfig(builder -> {
            builder.parent(parentPackage)
                    .controller(controllerPackage)
                    .entity(entityPackage)
                    .mapper(mapperPackage)
                    .pathInfo(Collections.singletonMap(OutputFile.xml,mapperXmlPath));
        })
                .strategyConfig(builder -> {
//模糊表匹配 ，可以选择生成指定表对应的代码
                    Arrays.stream(tablePrefix.split(","))
                            .forEach(prefix->builder.likeTable(new
                                    LikeTable(tablePrefix)));
                    builder.addTablePrefix(tablePrefix.split(","))
                            .controllerBuilder().enableFileOverride()
                            .enableRestStyle()
                            .serviceBuilder().enableFileOverride()
                            .mapperBuilder().enableBaseColumnList().enableBaseResultMap()
                            .enableFileOverride()
                            .entityBuilder().enableFileOverride()
                            .enableLombok().enableChainModel();
                }).templateEngine(new FreemarkerTemplateEngine())
                .execute();
    }
}
